package com.itheima.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Module;
import com.itheima.service.system.ModuleService;
import com.itheima.web.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "/system/module")
public class ModuleController extends BaseController {

    @Autowired
    private ModuleService moduleService;

    @RequestMapping(value = "/list", name = "分页查询模块列表")
    @RequiresPermissions("模块管理")
    public String findAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size){
        PageInfo<Module> pageInfo = moduleService.findByPage(page, size,companyId);
        request.setAttribute("page",pageInfo);
        return "system/module/module-list";
    }

    @RequestMapping(value = "/toUpdate", name = "跳转到模块修改页面")
    public String toUpdate(String id){
        Module module = moduleService.findById(id);
        List<Module> list = moduleService.findAll(companyId);
        request.setAttribute("menus",list);
        request.setAttribute("module", module);
        return "/system/module/module-update";
    }

    @RequestMapping(value = "/toAdd", name = "跳转到模块增加页面")
    public String toAdd(){
        List<Module> list = moduleService.findAll(companyId);
        request.setAttribute("menus",list);
        return "/system/module/module-add";
    }

    @RequestMapping(value = "/edit", name = "编辑模块信息")
    public String edit(Module module){
        if (StringUtils.isEmpty(module.getId())){
            module.setId(UUID.randomUUID().toString());
            moduleService.save(module);
        } else {
            moduleService.update(module);
        }
        return "redirect:/system/module/list.do";
    }

    @RequestMapping(value = "/delete", name = "删除模块信息")
    public String delete(String id){
        moduleService.delete(id);
        return "redirect:/system/module/list.do";
    }
}
